package logic

import (
	"backend/apps/book/models"
	"backend/apps/book/proto"
	"backend/apps/book/rpc/internal/svc"
	"context"
	"github.com/pkg/errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetAuthorLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetAuthorLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAuthorLogic {
	return &GetAuthorLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetAuthor 获取作者信息
func (l *GetAuthorLogic) GetAuthor(in *proto.IdRequest) (*proto.Author, error) {
	author, err := l.svcCtx.AuthorModel.FindOne(l.ctx, in.Id)
	if err != nil {
		if errors.Is(err, models.ErrNotFound) {
			return nil, errors.New("作者信息不存在")
		}
		return nil, err
	}

	return &proto.Author{
		Id:           author.Id,
		Name:         author.Name,
		AvatarUrl:    author.AvatarUrl,
		Introduction: author.Introduction.String,
	}, nil
}
